package site.luojie.business.user.service.impl;

import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import site.luojie.business.user.controller.param.UserLoginParam;
import site.luojie.business.user.controller.param.UserRegisterParam;
import site.luojie.business.user.controller.vo.UserVo;
import site.luojie.business.user.mapper.AuthTokenMapper;
import site.luojie.business.user.mapper.AuthUserMapper;
import site.luojie.business.user.pojo.AuthToken;
import site.luojie.business.user.pojo.AuthUser;
import site.luojie.business.user.service.UserService;
import site.luojie.common.service.CodeRemoteService;
import site.luojie.common.vo.ResultVo;

import java.time.LocalDateTime;
import java.util.UUID;

/**
 * @author jie.luo
 * @since 2020/9/17
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private AuthUserMapper authUserMapper;
    @Autowired
    private AuthTokenMapper authCodeMapper;
    @Reference
    private CodeRemoteService codeRemoteService;

    @Override
    public boolean checkExistUser(String email) {
        int i = authUserMapper.countAuthUser(email);
        return i > 0;
    }

    @Override
    public ResultVo<String> register(UserRegisterParam param) {

        // 校验验证码
        ResultVo<Boolean> resultVo = codeRemoteService.verifyCodeCheck(param.getEmail(), param.getCode());
        if (!StringUtils.equals(resultVo.getCode(), "200")) {
            return ResultVo.customData(resultVo.getCode(), resultVo.getMessage());
        }

        // 检测是否邮箱已经注册
        if (this.checkExistUser(param.getEmail())) {
            return ResultVo.customData("700", "邮箱已注册");
        }

        // 注册写入数据
        AuthUser user = new AuthUser();
        user.setEmail(param.getEmail());
        user.setPassword(param.getPassword());
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(user.getCreateTime());
        authUserMapper.insertAuthUser(user);

        return ResultVo.ok();
    }

    @Override
    public ResultVo<UserVo> login(UserLoginParam param) {

        AuthUser user = authUserMapper.selectAuthUser(param.getEmail());
        if (null == user) {
            return ResultVo.customData("701", "用户不存在");
        }

        if (!StringUtils.equals(user.getPassword(), param.getPassword())) {
            return ResultVo.customData("702", "用户名或密码错误");
        }

        UserVo userVo = new UserVo();
        userVo.setId(user.getId());
        userVo.setEmail(user.getEmail());
        userVo.setToken(UUID.randomUUID().toString());

        authCodeMapper.insertAuthToken(new AuthToken() {{
            this.setEmail(user.getEmail());
            this.setToken(userVo.getToken());
        }});
        return ResultVo.ok(userVo);
    }

    @Override
    public AuthUser getUser(String email) {
        return authUserMapper.selectAuthUser(email);
    }

    @Override
    public Boolean checkToken(String token) {
        return authCodeMapper.countAuthToken(token) > 0;
    }

}
